﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevTeam;
using SpreadsheetGear;
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;

namespace ConfExpert.Forms.Lojistik
{
    public partial class KoliUstuHF : XForm
    {
        public KoliUstuHF()
        {
            InitializeComponent();
        }

        private void KoliUstuHF_Load(object sender, EventArgs e)
        {
            btnAktar.ItemClick += btnAktar_ItemClick;
        }

        void btnAktar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (OpenFileDialog fdOpen = new OpenFileDialog())
            {
                DataTable dtKU = new DataTable("KU");
                foreach (string col in "Sayfa BoxNr Of SEASON BRAND TKZ DIVISION NAME ARTICLE COLOUR ORDER PO".Split(" ".ToCharArray()))
                    dtKU.Columns.Add(col, typeof(string));

                foreach (string col in "XS S M L XL XXL 35 36 37 38 39 40 41 42 43 44 45 46 Total".Split(" ".ToCharArray()))
                    dtKU.Columns.Add(col, typeof(int));

                fdOpen.Filter = "Excel Files (*.xls*;*.csv)|*.xls*;*.csv";
                fdOpen.FilterIndex = 1;
                fdOpen.Multiselect = false;
                if (fdOpen.ShowDialog(AppSession.MainForm) == DialogResult.OK)
                {
                    IWorkbook Workbook = SpreadsheetGear.Factory.GetWorkbook(fdOpen.FileName);
                    DataSet ds = Workbook.GetDataSet(SpreadsheetGear.Data.GetDataFlags.NoColumnHeaders);
                    foreach (DataTable dt in ds.Tables)
                        if (dt.TableName.StartsWith("PL ") && dt.TableName.Length<10)
                        {
                            DataRowCollection rows = dt.Rows;

                            int Season = 0, Size = 0, Total = 0;
                            for (int i = 0; i < rows.Count; i++)
                            {
                                for (int c = 0; c < dt.Columns.Count; c++)
                                    switch (dt.Rows[i][c].ToString().Trim())
                                    {
                                        case "SEASON": Season = i; break;
                                        case "SIZE": Size = c; break;
                                        case "total quant.": Total = c; break;
                                    }

                                if (Season > 0 && Size > 0 && Total > 0)
                                    break;
                            }

                            //bool SML = cmbDagilim.EditValue.ToString() == "SML";
                            int arabeden = 0;
                            for (int i = Season + 2; i < rows.Count; i++)
                            {
                                if (rows[i][2].ToString() == string.Empty)
                                    break;

                                for (int x = 11; x <= 21; x += 2)
                                    arabeden += rows[i][x].ToInt();
                            }
                            bool SML = arabeden == 0;
                            Notify(SML ? "SML" : "37");

                            for (int i = Season + 2; i < rows.Count; i++)
                            {
                                if (rows[i][2].ToString() == string.Empty)
                                    break;

                                DataRow r = dtKU.NewRow();
                                r["Sayfa"] = dt.TableName;
                                r["BoxNr"] = rows[i][0];
                                r["Of"] = rows[i][1];
                                r["SEASON"] = rows[i][2];
                                r["BRAND"] = rows[i][3];
                                r["TKZ"] = rows[i][4];
                                r["DIVISION"] = rows[i][5];
                                r["NAME"] = rows[i][6];
                                r["ARTICLE"] = rows[i][7];
                                r["COLOUR"] = rows[i][8];
                                r["ORDER"] = rows[i][9];
                                r["PO"] = rows[i][10];
                                r["Total"] = rows[i][23];
                                if (SML)
                                {
                                    r["XS"] = rows[i][12];
                                    r["S"] = rows[i][14];
                                    r["M"] = rows[i][16];
                                    r["L"] = rows[i][18];
                                    r["XL"] = rows[i][20];
                                    r["XXL"] = rows[i][22];
                                }
                                else for (int s = 35; s <= 46; s++)
                                        r[s.ToString()] = rows[i][11 + s - 35];

                                dtKU.Rows.Add(r);

                            }
                        }

                    string xml = dtKU.Serialize();
                    DataSet dsData = ExecuteDataSet("GetKoliUstuHolyFashion", xml);
                    Rapor.YeniYapi.KoliUstuHF hf = new Rapor.YeniYapi.KoliUstuHF(dsData);
                    hf.ShowPreviewDialog();
                }

            }
        }
    }
}